﻿#region Copyright (c) 2000-2013 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{                                                                   }
{                                                                   }
{       Copyright (c) 2000-2013 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2013 Developer Express Inc.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UColor.Core.Helpers;
using System.Windows.Data;
using System.Windows.Input;
using UColor.WPF.Helpers;
using System.Windows;
using System.Windows.Media;
namespace UColor.WPF.Internal {
	public sealed class BaseControlMainPage : BaseControlOrdinarPart {
		
		WeakEventHandler<ThePropertyChangedEventArgs<DemoModuleControlView>> onModuleWrapperDemoModuleViewChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<DemoModuleControlOptionsView>> onModuleWrapperOptionsViewChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onModuleWrapperIsOptionsExpandedChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onModuleWrapperLoadingInProgressChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<LString>> onModuleWrapperTitleChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onModuleWrapperAllowRtlChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onModuleWrapperAllowThemesChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onModuleWrapperHasOptionsChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<Visibility>> onModuleWrapperSidebarVisibilityChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<string>> onModuleWrapperSidebarTagChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<ImageSource>> onModuleWrapperSidebarIconChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<ImageSource>> onModuleWrapperSidebarIconSelectedChanged;
		WeakEventHandler<ModuleAppearEventArgs> onModuleWrapperModuleAppear;
		WeakEventHandler<ThePropertyChangedEventArgs<FlowDirection>> onModuleWrapperDemoFlowDirectionChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<FlowDirection>> onToolbarDemoFlowDirectionChanged;
		LString moduleTitle;
		bool allowRtl;
		Visibility sidebarVisibility = Visibility.Collapsed;
		string sidebarTag;
		ImageSource sidebarIcon;
		ImageSource sidebarIconSelected;
		BaseControlToobar toolbar;
		BaseControlModuleWrapper moduleWrapper;
		DemoModuleControlOptionsView optionsView;
		DemoModuleControlView demoModuleView;
		WeakEventHandler<ThePropertyChangedEventArgs<object>> onDemoBaseControlStateModuleIDChanged;
		
		bool isOptionsExpanded;
		FlowDirection demoFlowDirection;
		bool loadingInProgress;
		public BaseControlMainPage(BaseControl demoBaseControl)
			: base(demoBaseControl) {
			onDemoBaseControlStateModuleIDChanged = new WeakEventHandler<ThePropertyChangedEventArgs<object>>(OnDemoBaseControlStateModuleIDChanged);
			
			onModuleWrapperDemoModuleViewChanged = new WeakEventHandler<ThePropertyChangedEventArgs<DemoModuleControlView>>(OnModuleWrapperDemoModuleViewChanged);
			onModuleWrapperOptionsViewChanged = new WeakEventHandler<ThePropertyChangedEventArgs<DemoModuleControlOptionsView>>(OnModuleWrapperOptionsViewChanged);
			onModuleWrapperTitleChanged = new WeakEventHandler<ThePropertyChangedEventArgs<LString>>(OnModuleWrapperTitleChanged);
			onModuleWrapperAllowRtlChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnModuleWrapperAllowRtlChanged);
			onModuleWrapperIsOptionsExpandedChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnModuleWrapperIsOptionsExpandedChanged);
			onModuleWrapperAllowThemesChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnModuleWrapperAllowThemesChanged);
			onModuleWrapperHasOptionsChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnModuleWrapperHasOptionsChanged);
			onModuleWrapperSidebarVisibilityChanged = new WeakEventHandler<ThePropertyChangedEventArgs<Visibility>>(OnModuleWrapperSidebarVisibilityChanged);
			onModuleWrapperSidebarTagChanged = new WeakEventHandler<ThePropertyChangedEventArgs<string>>(OnModuleWrapperSidebarTagChanged);
			onModuleWrapperSidebarIconChanged = new WeakEventHandler<ThePropertyChangedEventArgs<ImageSource>>(OnModuleWrapperSidebarIconChanged);
			onModuleWrapperSidebarIconSelectedChanged = new WeakEventHandler<ThePropertyChangedEventArgs<ImageSource>>(OnModuleWrapperSidebarIconSelectedChanged);
			onModuleWrapperModuleAppear = new WeakEventHandler<ModuleAppearEventArgs>(OnModuleWrapperModuleAppear);
			onToolbarDemoFlowDirectionChanged = new WeakEventHandler<ThePropertyChangedEventArgs<FlowDirection>>(OnToolbarDemoFlowDirectionChanged);
			onModuleWrapperDemoFlowDirectionChanged = new WeakEventHandler<ThePropertyChangedEventArgs<FlowDirection>>(OnModuleWrapperDemoFlowDirectionChanged);
			onModuleWrapperLoadingInProgressChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnModuleWrapperLoadingInProgressChanged);
			Toolbar = CreateToolbar();
			ModuleWrapper = CreateWrapper();
			RaiseDemoFlowDirectionChanged(FlowDirection.LeftToRight, DemoFlowDirection);
            //ModuleWrapper.
			Initialized();
		}
		public event EventHandler<ModuleAppearEventArgs> ModuleAppear;
		protected override void BeginLoadData() {
			//DemoBaseControl.Data.LoadMainProduct(BeginInMainThread);
			base.BeginLoadData();
		}
		protected override void EndLoadData() {
			base.EndLoadData();
			
			
		}
		protected override void OnDemoBaseControlStateChanged(BaseControlState oldValue, BaseControlState newValue) {
			base.OnDemoBaseControlStateChanged(oldValue, newValue);
			if(oldValue != null)
				oldValue.ModuleIDChanged -= onDemoBaseControlStateModuleIDChanged.Handler;
			if(newValue != null) {
				newValue.ModuleIDChanged += onDemoBaseControlStateModuleIDChanged.Handler;
				OnDemoBaseControlStateModuleIDChanged(newValue, new ThePropertyChangedEventArgs<object>(null, newValue.ModuleID));
			}
		}
		public LString ModuleTitle {
			get { return moduleTitle; }
			private set { SetValue<LString>("ModuleTitle", ref moduleTitle, value, RaiseModuleTitleChanged); }
		}
		public bool AllowRtl {
			get { return allowRtl; }
			private set { SetValue<bool>("AllowRtl", ref allowRtl, value, RaiseAllowRtlChanged); }
		}
		public Visibility SidebarVisibility {
			get { return sidebarVisibility; }
			private set { SetValue<Visibility>("SidebarVisibility", ref sidebarVisibility, value, RaiseSidebarVisibilityChanged); }
		}
		public string SidebarTag {
			get { return sidebarTag; }
			private set { SetValue<string>("SidebarTag", ref sidebarTag, value, RaiseSidebarTagChanged); }
		}
		public ImageSource SidebarIcon {
			get { return sidebarIcon; }
			private set { SetValue<ImageSource>("SidebarIcon", ref sidebarIcon, value, RaiseSidebarIconChanged); }
		}
		public ImageSource SidebarIconSelected {
			get { return sidebarIconSelected; }
			private set { SetValue<ImageSource>("SidebarIconSelected", ref sidebarIconSelected, value, RaiseSidebarIconSelectedChanged); }
		}
		public DemoModuleControlOptionsView OptionsView {
			get { return optionsView; }
			set { SetValue<DemoModuleControlOptionsView>("OptionsView", ref optionsView, value, RaiseOptionsViewChanged); }
		}
		public DemoModuleControlView DemoModuleView {
			get { return demoModuleView; }
			set { SetValue<DemoModuleControlView>("DemoModuleView", ref demoModuleView, value, RaiseDemoModuleViewChanged); }
		}
		public bool IsOptionsExpanded {
			get { return isOptionsExpanded; }
			set { SetValue<bool>("IsOptionsExpanded", ref isOptionsExpanded, value, RaiseIsOptionsExpandedChanged); }
		}
		public FlowDirection DemoFlowDirection {
			get { return demoFlowDirection; }
			set { SetValue<FlowDirection>("DemoFlowDirection", ref demoFlowDirection, value, RaiseDemoFlowDirectionChanged); }
		}
		
		public BaseControlToobar Toolbar {
			get { return toolbar; }
			private set { SetValue<BaseControlToobar>("Toolbar", ref toolbar, value); }
		}
		public bool LoadingInProgress {
			get { return loadingInProgress; }
			private set { SetValue<bool>("LoadingInProgress", ref loadingInProgress, value, RaiseLoadingInProgressChanged); }
		}
		public BaseControlModuleWrapper ModuleWrapper {
			get { return moduleWrapper; }
			private set { SetValue<BaseControlModuleWrapper>("ModuleWrapper", ref moduleWrapper, value); }
		}
		public event ThePropertyChangedEventHandler<LString> ModuleTitleChanged;
		public event ThePropertyChangedEventHandler<DemoModuleControlView> DemoModuleViewChanged;
		public event ThePropertyChangedEventHandler<DemoModuleControlOptionsView> OptionsViewChanged;
		public event ThePropertyChangedEventHandler<bool> IsOptionsExpandedChanged;
		public event ThePropertyChangedEventHandler<FlowDirection> DemoFlowDirectionChanged;
		public event ThePropertyChangedEventHandler<bool> LoadingInProgressChanged;
		public event ThePropertyChangedEventHandler<bool> AllowRtlChanged;
		public event ThePropertyChangedEventHandler<Visibility> SidebarVisibilityChanged;
		public event ThePropertyChangedEventHandler<string> SidebarTagChanged;
		public event ThePropertyChangedEventHandler<ImageSource> SidebarIconChanged;
		public event ThePropertyChangedEventHandler<ImageSource> SidebarIconSelectedChanged;
		public void RequestNextDemo() {
            //Type t = typeof(UCOLOR.View.CartView);
            //ModuleWrapper.LoadModule(t);
            //if (Product == null || DemoBaseControl.State == null) return;
            //ModuleWrapper.IsNextDemoRequested = true;
            //DemoBaseControl.State.ModuleID = Product.GetNextModuleID(DemoBaseControl.State.ModuleID);
            ModuleWrapper.ReplaceCurrentModuleByNext();
		}
		public void RequestPrevDemo() {
            //Type t = typeof(UColor.Modules.GalleryPhotoViewer);
           // ModuleWrapper.LoadModule(t);
            ModuleWrapper.ReplaceCurrentModuleByPrev();
		}
		BaseControlToobar CreateToolbar() {
			BaseControlToobar toolbar = new BaseControlToobar(this, DemoBaseControl);
			toolbar.DemoFlowDirectionChanged += onToolbarDemoFlowDirectionChanged.Handler;
			return toolbar;
		}
		BaseControlModuleWrapper CreateWrapper() {
			BaseControlModuleWrapper wrapper = new BaseControlModuleWrapper(this, DemoBaseControl);
			wrapper.TitleChanged += onModuleWrapperTitleChanged.Handler;
			OnModuleWrapperTitleChanged(wrapper, new ThePropertyChangedEventArgs<LString>(null, wrapper.Title));
			wrapper.AllowRtlChanged += onModuleWrapperAllowRtlChanged.Handler;
			OnModuleWrapperAllowRtlChanged(wrapper, new ThePropertyChangedEventArgs<bool>(true, wrapper.AllowRtl));
			wrapper.SidebarVisibilityChanged += onModuleWrapperSidebarVisibilityChanged.Handler;
			wrapper.SidebarTagChanged += onModuleWrapperSidebarTagChanged.Handler;
			wrapper.SidebarIconChanged += onModuleWrapperSidebarIconChanged.Handler;
			wrapper.SidebarIconSelectedChanged += onModuleWrapperSidebarIconSelectedChanged.Handler;
			wrapper.ModuleAppear += onModuleWrapperModuleAppear.Handler;
			wrapper.DemoModuleViewChanged += onModuleWrapperDemoModuleViewChanged.Handler;
			OnModuleWrapperDemoModuleViewChanged(wrapper, new ThePropertyChangedEventArgs<DemoModuleControlView>(DemoModuleControlView.Demo, wrapper.DemoModuleView));
			wrapper.OptionsViewChanged += onModuleWrapperOptionsViewChanged.Handler;
			OnModuleWrapperOptionsViewChanged(wrapper, new ThePropertyChangedEventArgs<DemoModuleControlOptionsView>(DemoModuleControlOptionsView.Options, wrapper.OptionsView));
			wrapper.IsOptionsExpandedChanged += onModuleWrapperIsOptionsExpandedChanged.Handler;
			OnModuleWrapperIsOptionsExpandedChanged(wrapper, new ThePropertyChangedEventArgs<bool>(false, wrapper.IsOptionsExpanded));
			wrapper.AllowThemesChanged += onModuleWrapperAllowThemesChanged.Handler;
			OnModuleWrapperAllowThemesChanged(wrapper, new ThePropertyChangedEventArgs<bool>(false, wrapper.AllowThemes));
			wrapper.HasOptionsChanged += onModuleWrapperHasOptionsChanged.Handler;
			OnModuleWrapperHasOptionsChanged(wrapper, new ThePropertyChangedEventArgs<bool>(false, wrapper.HasOptions));
			wrapper.DemoFlowDirectionChanged += onModuleWrapperDemoFlowDirectionChanged.Handler;
			wrapper.LoadingInProgressChanged += onModuleWrapperLoadingInProgressChanged.Handler;
			OnModuleWrapperLoadingInProgressChanged(wrapper, new ThePropertyChangedEventArgs<bool>(false, wrapper.LoadingInProgress));
			return wrapper;
		}
		void OnModuleWrapperSidebarVisibilityChanged(object sender, ThePropertyChangedEventArgs<Visibility> e) {
			SidebarVisibility = e.NewValue;
		}
		void OnModuleWrapperSidebarTagChanged(object sender, ThePropertyChangedEventArgs<string> e) {
			SidebarTag = e.NewValue;
		}
		void OnModuleWrapperSidebarIconChanged(object sender, ThePropertyChangedEventArgs<ImageSource> e) {
			SidebarIcon = e.NewValue;
		}
		void OnModuleWrapperSidebarIconSelectedChanged(object sender, ThePropertyChangedEventArgs<ImageSource> e) {
			SidebarIconSelected = e.NewValue;
		}
		void OnModuleWrapperAllowRtlChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			AllowRtl = e.NewValue;
		}
		void OnModuleWrapperModuleAppear(object sender, ModuleAppearEventArgs e) {
			if(ModuleAppear != null)
				ModuleAppear(this, e);
		}
		void OnModuleWrapperTitleChanged(object sender, ThePropertyChangedEventArgs<LString> e) {
			ModuleTitle = e.NewValue;
		}
		
		void OnDemoBaseControlStateModuleIDChanged(object sender, ThePropertyChangedEventArgs<object> e) {
			//UpdateModule();
		}
		void RaiseLoadingInProgressChanged(bool oldValue, bool newValue) {
			if(LoadingInProgressChanged != null) {
				LoadingInProgressChanged(this, new ThePropertyChangedEventArgs<bool>(oldValue, newValue));
			}
		}
		void OnModuleWrapperLoadingInProgressChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			LoadingInProgress = e.NewValue;
		}
		void OnModuleWrapperDemoFlowDirectionChanged(object sender, ThePropertyChangedEventArgs<FlowDirection> e) {
			DemoFlowDirection = e.NewValue;
		}
		void OnToolbarDemoFlowDirectionChanged(object sender, ThePropertyChangedEventArgs<FlowDirection> e) {
			DemoFlowDirection = e.NewValue;
		}
		
		void RaiseModuleTitleChanged(LString oldValue, LString newValue) {
			if(ModuleTitleChanged != null)
				ModuleTitleChanged(this, new ThePropertyChangedEventArgs<LString>(oldValue, newValue));
		}
		void RaiseSidebarVisibilityChanged(Visibility oldValue, Visibility newValue) {
			if(SidebarVisibilityChanged != null)
				SidebarVisibilityChanged(this, new ThePropertyChangedEventArgs<Visibility>(oldValue, newValue));
		}
		void RaiseSidebarTagChanged(string oldValue, string newValue) {
			if(SidebarTagChanged != null)
				SidebarTagChanged(this, new ThePropertyChangedEventArgs<string>(oldValue, newValue));
		}
		void RaiseSidebarIconChanged(ImageSource oldValue, ImageSource newValue) {
			if(SidebarIconChanged != null)
				SidebarIconChanged(this, new ThePropertyChangedEventArgs<ImageSource>(oldValue, newValue));
		}
		void RaiseSidebarIconSelectedChanged(ImageSource oldValue, ImageSource newValue) {
			if(SidebarIconSelectedChanged != null)
				SidebarIconSelectedChanged(this, new ThePropertyChangedEventArgs<ImageSource>(oldValue, newValue));
		}
		void RaiseAllowRtlChanged(bool oldValue, bool newValue) {
			if(AllowRtlChanged != null) {
				AllowRtlChanged(this, new ThePropertyChangedEventArgs<bool>(oldValue, newValue));
			}
		}
		void RaiseDemoModuleViewChanged(DemoModuleControlView oldValue, DemoModuleControlView newValue) {
			if(ModuleWrapper != null)
				ModuleWrapper.DemoModuleView = newValue;
			if(DemoModuleViewChanged != null)
				DemoModuleViewChanged(this, new ThePropertyChangedEventArgs<DemoModuleControlView>(oldValue, newValue));
		}
		void RaiseOptionsViewChanged(DemoModuleControlOptionsView oldValue, DemoModuleControlOptionsView newValue) {
			if(ModuleWrapper != null)
				ModuleWrapper.OptionsView = newValue;
			if(OptionsViewChanged != null)
				OptionsViewChanged(this, new ThePropertyChangedEventArgs<DemoModuleControlOptionsView>(oldValue, newValue));
		}
		void RaiseIsOptionsExpandedChanged(bool oldValue, bool newValue) {
			if(ModuleWrapper != null)
				ModuleWrapper.IsOptionsExpanded = newValue;
			if(IsOptionsExpandedChanged != null)
				IsOptionsExpandedChanged(this, new ThePropertyChangedEventArgs<bool>(oldValue, newValue));
		}
		void RaiseDemoFlowDirectionChanged(FlowDirection oldValue, FlowDirection newValue) {
			Toolbar.DemoFlowDirection = newValue;
			ModuleWrapper.DemoFlowDirection = newValue;
			if(DemoFlowDirectionChanged != null)
				DemoFlowDirectionChanged(this, new ThePropertyChangedEventArgs<FlowDirection>(oldValue, newValue));
		}
		void OnModuleWrapperOptionsViewChanged(object sender, ThePropertyChangedEventArgs<DemoModuleControlOptionsView> e) {
			OptionsView = e.NewValue;
		}
		void OnModuleWrapperDemoModuleViewChanged(object sender, ThePropertyChangedEventArgs<DemoModuleControlView> e) {
			DemoModuleView = e.NewValue;
		}
		void OnModuleWrapperIsOptionsExpandedChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			IsOptionsExpanded = e.NewValue;
		}
		void OnModuleWrapperAllowThemesChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			Toolbar.AllowThemes = e.NewValue;
		}
		void OnModuleWrapperHasOptionsChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			Toolbar.HasOptions = e.NewValue;
		}
	}
}
